package com.woniu.commentorder.console;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.google.zxing.WriterException;
import com.hc.entity.PayRecord;
import com.hc.utils.ResponseEntity;
import com.hc.vo.QRCodeUtil;
import com.hc.vo.RQcodevo;
import com.woniu.commentorder.service.PayRecordService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.session.Session;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/payrecord")
@Slf4j
public class PayRecordConsole {
    @Autowired
    private OpenfeignConfig openfeignConfig;
    @Autowired
    private PayRecordService payRecordService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Value("${server.port}")
    private String test;


    @PostMapping("/add")
    @GlobalTransactional
    public ResponseEntity add(@RequestBody PayRecord payRecord) throws InterruptedException {

        payRecordService.add(payRecord);
        rabbitTemplate.convertSendAndReceive("orderexchange", "add.payRecord.afdad", payRecord.getId());
        return new ResponseEntity("生成订单成功");
    }


    @PostMapping("/write")
    public ResponseEntity write(@RequestBody RQcodevo rQcodevo) throws IOException, WriterException {
        QRCodeUtil.write(rQcodevo.getWritepath(), rQcodevo.getFilepath());
        return new ResponseEntity("图片保存成功");
    }

    @PostMapping("/parse")
    public ResponseEntity parse(@RequestBody RQcodevo rQcodevo) throws IOException, WriterException {
        return new ResponseEntity(QRCodeUtil.parse(rQcodevo.getFilepath()));
    }

    @GetMapping("/login")
    public String login(HttpSession session) {
        session.setAttribute("cookie","获取到session");
//        log.info("端口为" + test);
//        Student student = new Student();
//        student.setAge(3);
//        student.setName("张亮");
        return "登陆成功";
    }
    @GetMapping("/getsession")
    public String get(HttpSession session) {
        log.info("当前端口为"+test);
        return "获取到的session为"+session.getAttribute("cookie");
    }
}
